package com.cat.binaryAnswer;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/find-the-smallest-divisor-given-a-threshold/description/
 * @create 2025/8/11 08:34
 * @since JDK17
 */

public class Solution05 {
    boolean check(int[] nums, int target, int k) {
        int n = nums.length, sum = 0;
        for (int i = 0; i < n; i++) {
            sum += (nums[i] + k - 1) / k;
        }
        return sum <= target;
    }
    public int smallestDivisor(int[] nums, int threshold) {
        int l = -1, r = ((int) (1e6 + 1)), mid;
        while (l + 1 < r) {
            mid = l + (r - l) / 2;
            if (check(nums, threshold, mid)) {  // 满足要取更小
                r = mid;
            } else {
                l = mid;
            }
        }
        return r;
    }
}
